package com.xysoft.framework.entity;

public abstract class AbstractPagedCollection<T> implements PagedCollection<T> {

	private int pageNo;
	private int pageSize = -1;

	@Override
	public abstract int getTotalCount();

	@Override
	public int getTotalPage() {
		if (getTotalCount() <= 0) {
			return 0;
		}
		return getTotalCount() > 0 ? (getPageSize() < getTotalCount() ? (getTotalCount() % getPageSize() > 0 ? getTotalCount()
				/ getPageSize() + 1
				: getTotalCount() / getPageSize())
				: 1)
				: 0;
	}

	@Override
	public int getPageSize() {
		return this.pageSize == -1 ? getTotalCount() : this.pageSize;
	}

	@Override
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize < 1 ? 1 : pageSize;
	}

	@Override
	public int getPageNo() {
		return this.pageNo;
	}

	@Override
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo > 0 ? (pageNo >= getTotalPage() ? (getTotalPage() > 0 ? getTotalPage() - 1 : 0) : pageNo)
				: 0;
	}

	@Override
	public int getCount() {
		int extraCount = getPageNo()*getPageSize() + getPageSize() - getTotalCount();
		return extraCount > 0 ? getPageSize() - extraCount : getPageSize();
	}
}
